using System;
using System.Windows.Forms;
using System.Threading;

namespace UwinEducation.MySqlBackup
{
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.
            //ApplicationConfiguration.Initialize();
            //Application.Run(new MainForm());

            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // 设置全局异常处理
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            try
            {
                Application.Run(new MainForm());
            }
            catch (Exception ex)
            {
                MessageBox.Show($"应用程序发生未处理的异常：\n{ex.Message}\n\n请联系技术支持。", 
                    "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 处理UI线程异常
        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            MessageBox.Show($"应用程序发生异常：\n{e.Exception.Message}\n\n请联系技术支持。", 
                "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        // 处理非UI线程异常
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show($"应用程序发生严重异常：\n{(e.ExceptionObject as Exception)?.Message}\n\n请联系技术支持。", 
                "严重错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}